 <?php
					
include 'conn.php';		

$request_body = file_get_contents('php://input');
$data = json_decode($request_body, true);

//登录
if($_GET['lx']=='login'){

	if(strtolower($data['yzm'])!==$_SESSION['authcode']){

		$result["message"] ="验证码错误";
		$result["code"] = 201;
		echo json_encode($result);
		return;
	}

	//$sql="SELECT * from user where name='".$data['username']."'";		
	//$sql="SELECT *,user.name as uname FROM user ,juser  where user.juser_id=juser.id and user.name='".$data['username']."'";
	//是注意 两个表都有id时，只会用到二个的表的id!!!!
	$sql="SELECT * FROM juser,user  where user.juser_id=juser.id and user.name='".$data['username']."'";
	$jrows = array();
	$rs =  mysqli_query($con,$sql);
	$row = $rs->fetch_assoc();
	if($row){
		if($row['status']=='0'){

			$result["message"] ="用户被禁用，请联系管理员！";
			$result["code"] = 201;

		}elseif($row['password']==hash_hmac('md5', $data["password"], "SIGNATURE")){
			//$row['password']='';
			unset($row['password']);
			//unset($row['name(1)']);	
			$result["message"] ="登录成功！";
			$result["token"] =getToken($row["id"]);  //生成token!
			$result["refreshToken"] =refreshToken($row["id"]);  //生成token!
			$result["info"] =$row;
			$result["code"] = 200;
		

		}else{

			$result["message"] ="密码错误";
			$result["code"] = 201;
		}
	}else{
		$result["message"] ="用户不存在！";
		$result["code"] = 201;
	}
		
	echo json_encode($result);
	
}elseif($_GET['lx']=='register'){

	$sql="SELECT * FROM user where name='".$data['name']."'";
	$rs =  mysqli_query($con,$sql);
	$row = $rs->fetch_assoc();
	if($row){
		$result["message"] ="用户名已存在！";
		$result["code"] = 201;		
	}else{

		$passwd=hash_hmac('md5', $data["password"], "SIGNATURE");
		$sql="INSERT INTO user(name,phone,password,status)VALUES('";	
		$sql=$sql.$data["name"]."','";
		$sql=$sql.$data["phone"]."','";
		$sql=$sql.$passwd."','";
		$sql=$sql."0')";
		mysqli_query($con,$sql);

		$result["code"] = 200;
		$result["message"] ="用户注册成功！";
	}
	$result["sql"] = $sql;
	echo json_encode($result);
	
}elseif($_GET['lx']=='verify'){	

	//$result=verifyToken1("11");
	//echo json_encode($result);


	$token = $_SERVER['HTTP_AUTHORIZATION'];
	$token=str_replace("Bearer ","",$token);

	$str=verifyToken($token);

	$sql="SELECT * FROM juser,user  where user.juser_id=juser.id and user.id='".$str->iss."'";
	$rs =  mysqli_query($con,$sql);
	$row = $rs->fetch_assoc();
	
	unset($row['password']);
	$result['code'] = '200';
	$result['message'] = $row;
	echo json_encode($result);


}elseif($_GET['lx']=='info'){
	

//获取：角色和用户数据
}elseif($_GET['lx']=='getUserAndJuserList'){
	
	$sql="SELECT user.*,juser.name as zname,juser.menu_id from user LEFT JOIN juser ON juser.id=user.juser_id ";		
	$rows = array();
	$rs =  mysqli_query($con,$sql);
	while($row = $rs->fetch_assoc()){
		array_push($rows,$row);
	} 

	$sql="SELECT * from juser  ";		
	$jrows = array();
	$rs =  mysqli_query($con,$sql);
	while($row = $rs->fetch_assoc()){
		array_push($jrows,$row);
	} 

	$result["code"] = 200;
	$result["user"] = $rows;
	$result["juser"] = $jrows;
	$result["message"] = $sql;
	echo json_encode($result);

//更新：角色（添加，修改，删除） 用户（状态）
}elseif($_GET['lx']=='changeStatus'){
	
	if($_GET['table']=='juser'){
		$sql="UPDATE juser set ";
		foreach ($data as $k => $v) {		
			$sql=$sql. $k . "='" . $v . "',";
		}
		$sql=substr($sql,0,-1);		//去除最后一个字符 ','
		
		$sql=$sql." where id=".$data['id'];	
		
	}else{
		$sql="UPDATE user set status='".$data['status']."'  where id=".$data['id'];
	}
	
	mysqli_query($con,$sql);
	$result["message"] = "修改成功！";	
	$result["sql"] = $sql;			
	echo json_encode($result);
	
//删除:角色，用户  可以转到data.php
}elseif($_GET['lx']=='del'){
	
	$sql="DELETE FROM ".$data['table']." WHERE id=".$data['id'];
	mysqli_query($con,$sql);
	
	$result["code"] = 200;
	$result["message"] = "删除成功！";
	$result['sql']=$sql;
	echo json_encode($result);	
	
//获取 角色树（添加角色时）
}elseif($_GET['lx']=='getJuserTreeMenu'){
	
	$sql="select * from menu where parent_id =-1";

	$rows = array();
	$rs =  mysqli_query($con,$sql);
	while($row = $rs->fetch_assoc()){
	
		$result_child = array();
		$result_child=has_child($row['id']);

		if($result_child){
			$row['children']=$result_child;
		}else{
			$row['children']=[];
		}
		
		array_push($rows,$row);

		// $sql1="select * from menu where parent_id=".$row['id'];	
		// $rs1 =mysqli_query($con,$sql1);	
		// $result1 = array();
		// while($row1 = $rs1->fetch_assoc()){		
		// 	array_push($result1,$row1);
		// }
		// if($result1){
		// 	$row['children']=$result1;
		// }
		// array_push($rows,$row);
	} 
	
	$result["code"] = 200;
	$result["data"] = $rows;
	$result["message"] = "list";
	$result['sql']=$sql;
	echo json_encode($result);		
	
//获取 角色树（修改角色时）
}elseif($_GET['lx']=='getJuserMenuAddDel'){
	
	$sql="select * from menu where parent_id= -1";// ORDER BY issort
	$rows = array();
	$rs =  mysqli_query($con,$sql);
	while($row = $rs->fetch_assoc()){
		
		$juserID=$data['id'];	
		//二级菜单
		// $sql1="select * from menu where parent_id=".$row['id'];	
		// $rs1 =mysqli_query($con,$sql1);	
		// $result_child = array();
		// while($row_child = $rs1->fetch_assoc()){						

		// 	if(contains_($row_child['added'],$juserID))
		// 		$row_child['added']="1";	
		// 	else
		// 		$row_child['added']="0";	
			
		// 	if(contains_($row_child['del'],$juserID))
		// 		$row_child['del']="1";	
		// 	else
		// 		$row_child['del']="0";	
			
		// 	if(contains_($row_child['modify'],$juserID))
		// 		$row_child['modify']="1";	
		// 	else
		// 		$row_child['modify']="0";	
									
		// 	array_push($result_child,$row_child);
		// }
		// if($result_child)
		// 	$row['children']=$result_child;
		
		//二级菜单
		$result_child = array();
		$result_child=has_child2($row['id'],$juserID);

		if($result_child){
			$row['children']=$result_child;
		}else{
			$row['children']=[];
		}

//----------一级菜单----------
		if(contains_($row['added'],$juserID))
			$row['added']="1";	
		else
			$row['added']="0";	
		
		if(contains_($row['del'],$juserID))
			$row['del']="1";	
		else
			$row['del']="0";	
		
		if(contains_($row['modify'],$juserID))
			$row['modify']="1";	
		else
			$row['modify']="0";									

		array_push($rows,$row);
	} 
	
	$result["code"] = 200;
	$result["data"] = $rows;
	$result["message"] = $juserID;
	$result['sql']=$sql;
	echo json_encode($result);	

//新增，修改 角色	
}elseif($_GET['lx']=='juserEditSave'){

	if($_GET['type']=='add'){
		$sql="INSERT INTO juser(name,menu_id,description)value('";
		$sql=$sql.$data['name']."','";
		$sql=$sql.$data['value']."','";
		$sql=$sql.$data['description']."')";

	}elseif($_GET['type']=='edit'){
		$sql="UPDATE juser set name='".$data['name']."',menu_id='".$data['value']."',description='".$data['description']."'
			  where id=".$data['id'];
	}

	mysqli_query($con,$sql);	
	$result["code"] = 200;
	$result["message"] = "1！";
	$result['sql']=$sql;
	echo json_encode($result);	

//修改 角色 菜单权限
}elseif($_GET['lx']=='changeMenuSwitch'){
	$id=$data['id'];
	$juserID=$data['juserID'];
	$type=$data['type'];
	$check=$data['check'];

	$sql = "SELECT * FROM menu where id=".$id;
	$rs =  mysqli_query($con,$sql);
	$menu = $rs->fetch_assoc();

	if($check == "1" && $type == "add") { //switch ”选择，选中“
		$value_ = $menu["added"] . "," . $juserID;		
		$sql_ = "UPDATE menu set added='".$value_."' where id=".$id;
		
	} elseif ($check == "0" && $type == "add") {
		$value_=explode(',',$menu["added"]);
		$key = array_search($juserID, $value_);
		if ($key !== false) {
			unset($value_[$key]); //删除数组指定值
		}
		$value_=implode(',',$value_);
		$sql_ = "UPDATE menu set added='".$value_."' where id=".$id;		
	}

	if($check == "1" && $type == "del") { //switch ”选择，选中“
		$value_ = $menu["del"] . "," . $juserID;		
		$sql_ = "UPDATE menu set del='".$value_."' where id=".$id;
		
	} elseif ($check == "0" && $type == "del") {
		$value_=explode(',',$menu["del"]);
		$key = array_search($juserID, $value_);
		if ($key !== false) {
			unset($value_[$key]); //删除数组指定值
		}
		$value_=implode(',',$value_);
		$sql_ = "UPDATE menu set del='".$value_."' where id=".$id;		
	}

	if($check == "1" && $type == "modify") { //switch ”选择，选中“
		$value_ = $menu["modify"] . "," . $juserID;		
		$sql_ = "UPDATE menu set modify='".$value_."' where id=".$id;
		
	} elseif ($check == "0" && $type == "modify") {
		$value_=explode(',',$menu["modify"]);
		$key = array_search($juserID, $value_);
		if ($key !== false) {
			unset($value_[$key]); //删除数组指定值
		}
		$value_=implode(',',$value_);
		$sql_ = "UPDATE menu set modify='".$value_."' where id=".$id;		
	}
	mysqli_query($con,$sql_);

	$result["code"] = 200;		
	$result["message"] = "设置修改成功";
	$result['sql']=$sql_;
	echo json_encode($result);

//保存 用户修改数据
}elseif($_GET['lx']=='userEditSave'){
	
	$sql="UPDATE user set juser_id=".$data['juser_id'].",bm='".$data['bm']."',phone='".$data['phone']."'";	
	$sql=$sql." where id=".$data['id'];		
	mysqli_query($con,$sql);

	$result["code"] = 200;		
	$result["message"] = "修改成功！";
	$result['sql']=$sql;
	echo json_encode($result);
	
//重置 用户密码
}elseif($_GET['lx']=='changePassword'){
	
	$sql="UPDATE user set password='".$data['password']."'";	
	$sql=$sql." where id=".$data['id'];		
	mysqli_query($con,$sql);

	$result["code"] = 200;		
	$result["message"] = "密码修改成功！";
	$result['sql']=$sql;
	echo json_encode($result);


}elseif($_GET['lx']=='save'){
	
	if($_GET['addEdit']=='edit'){
		$sql="UPDATE js_9_vue_el set ";
		foreach ($data as $k => $v) {		
			$sql=$sql. $k . "='" . $v . "',";
		}
		$sql=substr($sql,0,-1);		//去除最后一个字符 ','
		
		$sql=$sql." where id=".$data['id'];		
		
		$result["message"] = "修改成功！";

	}elseif($_GET['addEdit']=='add'){

		$sql="INSERT INTO js_9_vue_el(";	
	
		foreach ($data as $k => $v) {		
			$cols=$cols.$k.",";
			$value=$value."'".$v."',";
		}
		$cols=substr($cols,0,-1);	
		$value=substr($value,0,-1);

		$sql=$sql.$cols.")VALUES(".$value.")";

		$result["message"] = "添加成功！";
	}

	mysql_query($sql);
	
	$result["code"] = 200;		
	$result['sql']=$sql;
	echo json_encode($result);
	
}elseif($_GET['lx']=='del1'){
			
	$sql="DELETE FROM js_9_vue_el WHERE id=".$data['id'];
	mysql_query($sql);
	
	$result["code"] = 200;
	$result["message"] = "删除成功！";
	$result['sql']=$sql;
	echo json_encode($result);
	
}else{
	$result["code"] = 201;
	$result["message"] = "else";
	echo json_encode($result);
	
}
  
function has_child($id){
	global  $con;

	$result = array();
	$sql="select * from menu where parent_id=".$id;	
	$rs_child =mysqli_query($con,$sql);	
	while($row = $rs_child->fetch_assoc()){
		
		$result_child = array();
		$result_child = has_child($row['id']);
	
		if($result_child){
			$row['children']=$result_child;
		}else{
			$row['children']=[];
		}	
		
		array_push($result,$row);		
	}	
	//echo $sql."<br>";
	return $result;
} 

function has_child2($id,$juserID){
	global  $con;

	$result = array();
	$sql="select * from menu where parent_id=".$id;	
	$rs_child =mysqli_query($con,$sql);	
	while($row_child = $rs_child->fetch_assoc()){
		
		$result_child = array();
		$result_child = has_child2($row_child['id'],$juserID);
	
		if(contains_($row_child['added'],$juserID))
			$row_child['added']="1";	
		else
			$row_child['added']="0";	
		
		if(contains_($row_child['del'],$juserID))
			$row_child['del']="1";	
		else
			$row_child['del']="0";	
		
		if(contains_($row_child['modify'],$juserID))
			$row_child['modify']="1";	
		else
			$row_child['modify']="0";	

		if($result_child){
			$row_child['children']=$result_child;
		}else{
			$row_child['children']=[];
		}			

		array_push($result,$row_child);		
	}	
	//echo $sql."<br>";
	return $result;
} 

?>